package cn.jiakeyun.umeng_vertify_example

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.umeng.umverify.UMConstant


class AgreementActivity : Activity() {

    val backImg by lazy {
        findViewById<ImageView>(R.id.back_btn)
    }

    val mWeb by lazy {
        findViewById<WebView>(R.id.web_view)
    }
    val mHeaderText by lazy {
        findViewById<TextView>(R.id.header_text)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_agreement)
        setAndroidNativeLightStatusBar(this,true)
        val mUrl = intent.getStringExtra(UMConstant.PROTOCOL_WEBVIEW_URL)
        val mName = intent.getStringExtra(UMConstant.PROTOCOL_WEBVIEW_NAME)
        mHeaderText.run {
            text = mName
        }
        backImg.setOnClickListener {
            finish()
        }
        initWebView()
        mUrl?.run {
            mWeb.loadUrl(this)
        }

    }

    private fun setAndroidNativeLightStatusBar(activity: Activity, dark: Boolean) {
        val decor = activity.window.decorView
        if (dark) {
            decor.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decor.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebView() {
        mWeb.webViewClient = object: WebViewClient(){
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                url:String
            ): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
        val wvSettings: WebSettings = mWeb.settings
        // 是否阻止网络图像
        wvSettings.blockNetworkImage = false
        // 是否阻止网络请求
        wvSettings.blockNetworkLoads = false
        // 是否加载JS
        wvSettings.javaScriptEnabled = true
        wvSettings.javaScriptCanOpenWindowsAutomatically = true
        //覆盖方式启动缓存
        wvSettings.cacheMode = WebSettings.LOAD_DEFAULT
        // 使用广泛视窗
        wvSettings.useWideViewPort = true
        wvSettings.loadWithOverviewMode = true
        wvSettings.domStorageEnabled = true
        //是否支持缩放
        wvSettings.builtInZoomControls = false
        wvSettings.setSupportZoom(false)
        //不显示缩放按钮
        wvSettings.displayZoomControls = false
        wvSettings.allowFileAccess = true
        wvSettings.databaseEnabled = true
        //缓存相关
        wvSettings.setAppCacheEnabled(true)
        wvSettings.domStorageEnabled = true
        wvSettings.databaseEnabled = true
    }

}